#include "mainwidget.h"
#include "userinfowidget.h"
#include "model/datacenter.h"

#include <QMessageBox>

UserInfoWidget::UserInfoWidget(const UserInfo& user, QWidget* parent):
    QDialog(parent), m_self(user)
{
    setFixedSize(288, 150);
    setWindowIcon(QIcon(":/resource/image/logo.png"));
    setWindowTitle("用户信息");
    // 设置关闭后⾃动销毁
    setAttribute(Qt::WA_DeleteOnClose);
    // 设置窗口出现的位置
    move(QCursor::pos());
    setStyleSheet("QWidget { background-color: #FFFFFF; }"
                  "QPushButton { border: 1px solid; border-radius: 5px; padding: 5px; background-color: #F2F2F2; }"
                  "QPushButton:pressed { background-color: #C4C4C4; }");

    // 布局设置
    QGridLayout *layout = new QGridLayout();
    layout->setSpacing(15);
    layout->setContentsMargins(0, 10, 0, 0);
    layout->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
    setLayout(layout);

    // 1.设置头像
    QPushButton* avatar = new QPushButton();
    avatar->setFixedSize(75, 75);
    avatar->setIconSize(QSize(75, 75));
    avatar->setIcon(user.m_avatar);
    avatar->setStyleSheet("QPushButton { border: none; background-color: white; }");

    // 2.设置用户序号
    QLabel* idTag = new QLabel();
    idTag->setText("序号");
    idTag->setAlignment(Qt::AlignCenter);
    idTag->setStyleSheet("QLabel { font-weight: 800; }");
    QLabel* idLabel = new QLabel();
    idLabel->setText(user.m_userId);

    // 3.设置用户昵称
    QLabel* nameTag = new QLabel();
    nameTag->setText("昵称");
    nameTag->setAlignment(Qt::AlignCenter);
    nameTag->setStyleSheet("QLabel { font-weight: 800; }");
    QLabel* nameLabel = new QLabel();
    nameLabel->setText(user.m_nickName);

    // 4.设置用户电话
    QLabel* phoneTag = new QLabel();
    phoneTag->setText("电话");
    phoneTag->setAlignment(Qt::AlignCenter);
    phoneTag->setStyleSheet("QLabel { font-weight: 800; }");
    QLabel* phoneLabel = new QLabel();
    phoneLabel->setText(user.m_phone);

    m_applybtn = new QPushButton("申请好友");
    m_sendbtn = new QPushButton("发送消息");
    m_deletebtn = new QPushButton("删除好友");

    layout->addWidget(avatar, 0, 0, 3, 1);
    layout->addWidget(idTag, 0, 1);
    layout->addWidget(idLabel, 0, 2);
    layout->addWidget(nameTag, 1, 1);
    layout->addWidget(nameLabel, 1, 2);
    layout->addWidget(phoneTag, 2, 1);
    layout->addWidget(phoneLabel, 2, 2);

    layout->addWidget(m_applybtn, 3, 0);
    layout->addWidget(m_sendbtn, 3, 1);
    layout->addWidget(m_deletebtn, 3, 2);

    // 设置按钮工作状态
    // 是好友，可以发送消息；不是好友，可以申请消息
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    // 到好友列表理遍历
    auto f = dataCenter->getFriendById(user.m_userId);
    // 设置按钮状态
    if(f){
        m_applybtn->setEnabled(false);
    }else{
        m_sendbtn->setEnabled(false);
        m_deletebtn->setEnabled(false);
    }

    // 初始化槽函数
    initSignalslot();
}

void UserInfoWidget::initSignalslot()
{
    MainWidget* widget = MainWidget::getInstance();
    connect(m_sendbtn, &QPushButton::clicked, widget, [=]{
        widget->switchToSession(m_self.m_userId);
        this->close();
    });

    connect(m_deletebtn, &QPushButton::clicked, this, [=]{
        // 1. 弹出对话框确认是否要删除
        auto reply = QMessageBox::warning(this,"提示", "确认是否删除?", QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok);
        if(reply == QMessageBox::Cancel){
            LOG() << "取消删除";
            return ;
        }
        // 2.发送网络请求来测试
        model::DataCenter* dataCenter = model::DataCenter::getInstance();
        dataCenter->deleteFriendAsync(m_self.m_userId);

        this->close();
    });

    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(m_applybtn, &QPushButton::clicked, dataCenter, [=]{
        dataCenter->addFriendApplyAsync(m_self.m_userId);
        this->close();
    });
}
